<template>
	<ui-sys :title="this.navTitle">

		<view class="writeBox">
			<view class="card">
				<view class="vertical"></view>
				<view class="content">{{ questionInfo.title }}</view>
			</view>
			<view class="editor" v-show="writeType === 'RichText'">
				<!-- 撰写答案 -->
				<robin-editor ref="RichText" class="editor" :value="questionInfo.content" width="95%" :muiltImage="true"
					:imageUploader="uploadImg" :header="false"></robin-editor>
			</view>
			<!-- MarkDown编辑 -->
			<view class="markDownEditor" v-show="writeType === 'MarkDown'">
				<robin-editor :header="false" ref="MarkDown" :value="questionInfo.content" class="editor" width="95%"
					:imageUploader="uploadImg" :autoHideToolbar="true"></robin-editor>
			</view>
			<view class="controlMenu">
				<!-- 切换撰写状态 -->
				<view class="changeWriteType">
					<text class="cicon-sync-alt text-blue"></text>
				</view>
				<view class="submit">
					<text class="cicon-done text-white"></text>
				</view>
			</view>
		</view>
	</ui-sys>
</template>

<script>
	import {
		userInfoRequest
	} from '@/api/login.js';

	import {
		explainByEidRequest,
		questionByQidRequest,
	} from '@/api/question.js'
	import {
		getStorageUserInfo
	} from '@/utils/storageUtils.js';

	export default {

		onLoad: function(option) {
			//option为object类型，会序列化上个页面传递的参数
			// console.log(option.userId); //打印出上个页面传递的参数。
			// console.log(option.questionId); //打印出上个页面传递的参数。
			// console.log(option.explanationIDBySelf); //打印出上个页面传递的参数。
			if (option.eid) {
				this.getExplanation(option.eid)
			}else{
				this.getQuestion(option.qid)
			}
			if (option.explanationIDBySelf == 'true') {
				this.navTitle = '修改题解'
			} else {
				this.navTitle = '写题解'
			}

		},
		mounted() {

		},
		data() {
			return {
				writeType: 'RichText',
				navTitle: '',
				questionInfo: {
					_id: null,
					title: '',
					content: ''
				},
			};
		},
		methods: {
			getExplanation(eid) {
						//	let _this = this
				const param = {
					eid
				}
				explainByEidRequest(param).then(res => {
					if (res.code == 200) {
						this.questionInfo.qid = res.data.questionMiniVO.qid 
						this.questionInfo.title = res.data.questionMiniVO.title
						this.questionInfo.content = res.data.content
					} else {
						this.$Tips(res.msg);
					}
				})
			},
			getQuestion(qid) {
							// let _this = this
				const param = {
					qid
				}
				questionByQidRequest(param).then(res => {
					if (res.code == 200) {
					this.questionInfo.qid = res.data.qid
					this.questionInfo.title = res.data.title
					this.questionInfo.content = ''
					} else {
						this.$Tips(res.msg);
					}
				})
			},
			uploadImg(img, callback) {
				console.log(img)
			}
		},

	}
</script>

<style lang="scss">
	view {
		box-sizing: border-box;
	}

	.writeBox {
		padding-top: 20rpx;
		// height: 100vh;
		// overflow: hidden;
	}

	.card {
		position: sticky;
		top: 0;
		display: flex;
		justify-content: flex-start;
		align-items: flex-start;
		width: 95%;
		margin: 0 auto;
		padding: 20rpx;
		background-color: #efecf2;
		font-size: 30rpx;
		border-radius: 8px;

		.vertical {
			position: absolute;
			top: 36rpx;
			left: 20rpx;
			width: 8rpx;
			height: 24rpx;
			background: linear-gradient(180deg, #91abf6 0%, #5873e9 100%);
			border-radius: 1px;
		}

		.content {
			font-weight: bold;
			max-width: 632rpx;
			line-height: 50rpx;
			margin-left: 20rpx;
		}

	}

	.editor {
		width: 690rpx;
		margin: 0 auto;
		border-radius: 8px;
		margin-top: 10rpx;
	}

	.markDownEditor {
		width: 690rpx;
		margin: 0 auto;
		border-radius: 8px;
		margin-top: 10rpx;
		background-color: #efecf2;

		editor {
			padding: 20rpx;
		}
	}

	.controlMenu {
		width: 690rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		position: fixed;
		bottom: 30rpx;
		left: 50%;
		transform: translateX(-50%);

		.changeWriteType {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 96rpx;
			height: 96rpx;
			border: 2rpx solid #0069fa;
			border-radius: 50%;
			background: #fff;
			box-shadow: 0px 4px 11px 0px rgba(38, 46, 99, 0.15);

			&:active {
				opacity: 0.7;
			}
		}

		.submit {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100rpx;
			height: 100rpx;
			background: #0069fa;
			border-radius: 50%;
			margin-left: 20rpx;
			box-shadow: 0px 4px 11px 0px rgba(38, 46, 99, 0.15);

			&:active {
				opacity: 0.7;
			}
		}
	}
</style>
